package cn.haiway.drools.service;

import cn.haiway.drools.pojo.Rule;
import cn.haiway.drools.respository.RuleRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class RuleService{

    @Autowired
    private RuleRepository ruleRepository;

    public Rule save(Rule rule) {
        log.debug("Request to save Rule : {}", rule);
        return ruleRepository.save(rule);
    }

    public Page<Rule> findAll(Pageable pageable) {
        log.debug("Request to get all Rules");
        return ruleRepository.findAll(pageable);
    }

    public Optional<Rule> findOne(String id) {
        log.debug("Request to get Rule : {}", id);
        return ruleRepository.findById(id);
    }

    public List<Rule> findByProjectId(String projectId){
        Rule rule = new Rule();
        rule.setProjectId(projectId);
        Example<Rule> ruleExample = Example.of(rule);
        List<Rule> rules = ruleRepository.findAll(ruleExample);

        return rules;
    }
}
